﻿
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class Monster : MonoBehaviour
{
    public const float SCALE_MAX = 10;
    public const float SCALE_MIN = 1;
    public const int SCORE_PER_UNIT = 500;


    public const float MAX_MOVE_SPEED = 3;
    public const float MIN_MOVE_SPEED = 1;

    public const float MAX_SCALE = 5;
    public const float MIN_SCALE = 1;

    public event Action<Monster, Monster[]> SplitEvent;
    public event Action<Monster> TriggerMonsterEvent;

    public bool isOnline
    {
        get { return m_isOnline; }
        set
        {
            if (m_isOnline) m_spriteRenderer.color = Color.white;
            else m_spriteRenderer.color = Color.gray;
            m_isOnline = value;
        }
    }
    private bool m_isOnline = false;

    public int listIndex { get { return m_listIndex; } }//用于标记盖怪物处于 player 怪物列表索引
    private int m_listIndex = -1;

    public string userId { get { return m_userId; } }
    private string m_userId;

    public int spriteIndex { get { return m_spriteIndex; } }
    private int m_spriteIndex = -1;

    public int score { get { return m_score; } }
    [SerializeField]
    private int m_score = SCORE_PER_UNIT;

    public float curScale { get { return m_curscale; } }
    private float m_curscale = SCALE_MIN;

    [SerializeField]
    private SpriteRenderer m_spriteRenderer;

    private Vector2 m_direction;

    private void Update()
    {

    }
    private void FixedUpdate()
    {
        var scaleOffset = (float)m_score / SCORE_PER_UNIT;
        var scale = MIN_SCALE * scaleOffset;

        if (scale > MAX_SCALE) scale = MAX_SCALE;
        this.transform.localScale = Vector3.one * scale;

        var speedOffset = SCORE_PER_UNIT / (float)m_score;
        var speed = MIN_MOVE_SPEED + (MAX_MOVE_SPEED - MIN_MOVE_SPEED) * speedOffset;
        if (speed > MAX_MOVE_SPEED) speed = MAX_MOVE_SPEED;
        this.GetComponent<Rigidbody>().velocity = m_direction.normalized * MAX_MOVE_SPEED;

        // Debug.Log(scale + "  ----  " + speed);

    }

    public void SetDirection(Vector2 pDirection)
    {
        m_direction = pDirection;
    }
    public void SetScore(int pScore)
    {
        m_score = pScore;
    }

    public void SetUserId(string pUserId)
    {
        m_userId = pUserId;
    }
    public void SetSprite(Sprite pSprite, int pSpriteIndex)
    {
        m_spriteRenderer.sprite = pSprite;
        m_spriteIndex = pSpriteIndex;
    }

    public Monster[] Split()
    {
        Debug.LogWarning("Split");
        var splitCount = 2;
        if (m_score >= splitCount * SCORE_PER_UNIT)
        {
            List<Monster> splitMonsters = new List<Monster>();
            for (int index = 0; index < splitCount; index++)
            {
                var item = MonsterPorcessor.instance.CreateMonster(m_userId, m_spriteIndex);
                item.SetScore(m_score / splitCount);
                splitMonsters.Add(item);
            }
            var anlge = 360f / splitMonsters.Count;
            var start = UnityEngine.Random.Range(0, 360);
            for (var index = 0; index < splitMonsters.Count; index++)
            {
                var dis = splitMonsters[index].GetComponent<SphereCollider>().radius + this.GetComponent<SphereCollider>().radius * 0.8f;
                var curangle = start + index * anlge;
                Vector2 pos = new Vector2(dis * Mathf.Cos(curangle), dis * Mathf.Sin(curangle));
                splitMonsters[index].transform.position = (Vector2)this.transform.position + pos;
                //splitMonsters[index].GetComponent<Rigidbody>().AddForce(pos * 100);
            }
            return splitMonsters.ToArray();
        }
        else
        {
            return null;
        }
    }



    private void OnTriggerEnter(Collider other)
    {

        if (other.gameObject.layer == 9)//遇到食物
        {
            m_score += other.gameObject.GetComponent<Food>().score;
            other.gameObject.GetComponent<Food>().BeEaton(this);
        }
        else if (other.gameObject.layer == 10)//遇到刺
        {
            var halfScre = m_score / 2;
            var tempScore = halfScre;

            List<Monster> splitMonsters = new List<Monster>();
            while (tempScore > 2 * SCORE_PER_UNIT)
            {
                var item = MonsterPorcessor.instance.CreateMonster(m_userId, m_spriteIndex);
                splitMonsters.Add(item);
                tempScore -= SCORE_PER_UNIT;
            }
            if (splitMonsters.Count == 0)
            {
                return;
            }

            splitMonsters.Insert(0, this);



            //考虑分裂路径------
            this.SetScore(halfScre);
            var anlge = 360f / splitMonsters.Count;
            var start = UnityEngine.Random.Range(0, 360);
            for (var index = 0; index < splitMonsters.Count; index++)
            {
                var dis = splitMonsters[index].GetComponent<SphereCollider>().radius + this.GetComponent<SphereCollider>().radius * 0.8f;
                var curangle = start + index * anlge;
                Vector2 pos = new Vector2(dis * Mathf.Cos(curangle), dis * Mathf.Sin(curangle));
                splitMonsters[index].transform.position = (Vector2)this.transform.position + pos;
                //splitMonsters[index].GetComponent<Rigidbody>().AddForce(pos * 100);
            }
        }
        else if (other.gameObject.layer == 11)//遇到怪物
        {
            Debug.Log("遇到怪物 :" + other.gameObject.layer);
        }
        else if (other.gameObject.layer == 12)  //撞墙
        {
            Debug.Log("撞墙 :" + other.gameObject.layer);
        }
        else
        {
            Debug.LogError("Qy layer wrong !!!");
        }
    }
    private void OnCollisionEnter(Collision collision)
    {
        //Debug.Log("OnCollisionEnter :" + collision.gameObject.layer);

        if (collision.gameObject.layer == 11)//遇到怪物
        {
            //var otherMonster = other.GetComponent<Monster>();
            //if (otherMonster.userId != m_userId)
            //{
            //    //var ownDis = (Vector2)(other.ClosestPoint() - this.transform.position);
            //    // var otherDis = (Vector2)(other.ClosestPoint() - this.transform.position);
            //    // this.transform.position -= oth
            //}

        }
        else if (collision.gameObject.layer == 12)
        {

        }
        else
        {
            Debug.LogError("Qy layer wrong !!!");
        }
    }


}
